//
//  SAStatisticsSaleVC.m
//  saasRetail
//
//  Created by mac on 2022/7/21.
//

#import "SAStatisticsSaleVC.h"
#import "SAStatisticsSaleCell.h"
#import "SAProductSalesVolumeAPI.h"
#import "SAProductSalesVolumeModel.h"
#import "HDDatePickerView.h"
#import "YBImageBrowser.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "ScanHelper.h"
#import "SASupplierScreeningView3.h"
#import "SAProductGetConditionAPI.h"
#import "SAInventorySortView.h"

#define kTabBarTitle @[@"默认",@"实销数降序",@"实销数升序",@"实销数额降序",@"实销额升序",@"毛利降序",@"毛利升序"]

@interface SAStatisticsSaleVC ()<UITableViewDelegate, UITableViewDataSource,HTTPAPIDelegate,UITextFieldDelegate,YBImageBrowserDataSource>

@property (nonatomic, strong) NSString *type;

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UITextField *searchTf;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIButton *scanBtn;
@property (nonatomic, strong) UIButton *moreBtn;
@property (nonatomic, strong) UIView *moreView;
@property (nonatomic, strong) UIButton *screeningBtn;
@property (nonatomic, strong) UIView *screeningLine;
@property (nonatomic, strong) UIButton *sortingBtn;
@property (nonatomic, strong) UIView *sortingLine;

@property (nonatomic, strong) UIView *topContentView;
@property (nonatomic, strong) UILabel *startLab;
@property (nonatomic, strong) UILabel *startDateLab;
@property (nonatomic, strong) UILabel *endLab;
@property (nonatomic, strong) UILabel *endDateLab;
@property (nonatomic, strong) UIImageView *iconImageV;
@property (nonatomic, strong) UIButton *startDateBtn;
@property (nonatomic, strong) UIButton *endDateBtn;
@property (nonatomic, strong) UIView *tipView;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;

@property (nonatomic, strong) NSMutableArray<SAProductSalesVolumeModel *> *dataMarr;
@property (nonatomic, strong) SAProductSalesVolumeAPI *productSalesVolumeAPI;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSArray *imageList;
@property (nonatomic, strong) SAProductGetConditionAPI *productGetConditionAPI;
@property (nonatomic, strong) NSMutableArray *screenArray;
@property (nonatomic, strong) NSString *providerId;
@property (nonatomic, strong) NSString *providerName;

@end

@implementation SAStatisticsSaleVC

-(instancetype)initWithType:(NSString *)type {
    if (self = [super init]) {
        self.type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    if (self.productSalesVolumeAPI == api) {
        if (self.currentPage == 1) {
            [self.dataMarr removeAllObjects];
        }
        NSMutableArray<SAProductSalesVolumeModel *> *marr = [SAProductSalesVolumeModel mj_objectArrayWithKeyValuesArray:data[@"data"][@"records"]];
        for (SAProductSalesVolumeModel *model in marr) {
            if (model.colorName.length == 0) {
                model.colorName = @"均色";
            }
            if (model.sizeName.length == 0) {
                model.sizeName = @"均码";
            }
            [self.dataMarr addObject:model];
        }
        [self.tableView reloadData];
        self.tipView.hidden = self.dataMarr.count > 0;
    }else if (self.productGetConditionAPI == api) {
        NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
        NSString *crossStoreProduct = [NSString stringWithFormat:@"%@",menuList[1][@"perms"][@"crossStoreProduct"]];
        [self.screenArray removeAllObjects];
        for ( NSDictionary *dict in (NSArray *)data[@"data"]) {
            SAProductHomeScreeningModel *model = [SAProductHomeScreeningModel mj_objectWithKeyValues:dict];
            if ([model.name isEqualToString:@"店铺"] && [crossStoreProduct isEqualToString:@"0"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.attrId == [SAValueManger shareInStance].userInfo.merchantId) {
                        list.check = YES;
                    }
                }
                [self.screenArray addObject:model];
            }else if ([model.name isEqualToString:@"类别"]) {
                if (model.voList.count > 0) {
                    model.voList[0].check = YES;
                }
                [self.screenArray addObject:model];
            }else if ([model.name isEqualToString:@"季节"]) {
                if (model.voList.count > 0) {
                    model.voList[0].check = YES;
                }
                [self.screenArray addObject:model];
            }else if ([model.name isEqualToString:@"品牌"]) {
                if (model.voList.count > 0) {
                    model.voList[0].check = YES;
                }
                [self.screenArray addObject:model];
            }
        }
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataMarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WeakSelf
    SAStatisticsSaleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAStatisticsSaleCell" forIndexPath:indexPath];
    SAProductSalesVolumeModel *model = self.dataMarr[indexPath.row];
    [cell refreshWithModel:model AndType:self.type];
    cell.showImageBlock = ^{
        [weakSelf showImage:model.imgList];
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.searchTf.isEditing == YES) {
        [self.view endEditing:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(71);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

#pragma mark - UITextField
- (void)textFieldEditChanged:(UITextField *)textField {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchProduct:) object:textField];
    [self performSelector:@selector(searchProduct:) withObject:textField afterDelay:0.3];
}

- (void)searchProduct:(UITextField *)textField {
    self.currentPage = 1;
    [self loadData];
}

#pragma mark - event
-(void)loadData {
    
    self.productSalesVolumeAPI.currentPage = self.currentPage;
    self.productSalesVolumeAPI.providerId = @"";
    self.productSalesVolumeAPI.productNameOrCode = @"";
    
    if (self.searchTf.text.length > 0) {
        self.productSalesVolumeAPI.productNameOrCode = self.searchTf.text;
    }
    if (self.providerId.length > 0) {
        self.productSalesVolumeAPI.providerId = self.providerId;
    }
    
    self.productSalesVolumeAPI.categoryId = @"";
    self.productSalesVolumeAPI.seasonId = @"";
    self.productSalesVolumeAPI.brandId = @"";
    
    self.productSalesVolumeAPI.merchantId = @([SAValueManger shareInStance].userInfo.merchantId).stringValue;
    
    if (self.screenArray.count >0) {
        for (SAProductHomeScreeningModel *model in self.screenArray) {
            if ([model.name isEqualToString:@"店铺"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES) {
                        self.productSalesVolumeAPI.merchantId = [NSString stringWithFormat:@"%ld",list.attrId];
                    }
                }
            }else if ([model.name isEqualToString:@"类别"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES &&![list.attrName isEqualToString:@"全部"]) {
                        self.productSalesVolumeAPI.categoryId = [NSString stringWithFormat:@"%ld",list.attrId];
                    }
                }
            }else if ([model.name isEqualToString:@"季节"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES &&![list.attrName isEqualToString:@"全部"]) {
                        self.productSalesVolumeAPI.seasonId = [NSString stringWithFormat:@"%ld",list.attrId];
                    }
                }
            }else if ([model.name isEqualToString:@"品牌"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES &&![list.attrName isEqualToString:@"全部"]) {
                        self.productSalesVolumeAPI.brandId = [NSString stringWithFormat:@"%ld",list.attrId];
                    }
                }
            }
        }
    }
    
    [self.productSalesVolumeAPI sendRequestToServerWithActivityViewAndMask:GET];
}

- (void)clickStartDateBtn:(UIButton *)btn {
    WeakSelf
    HDDatePickerView *pickerView = [[HDDatePickerView alloc] initWithSlectedDate:self.startDateLab.text];
    [pickerView show];
    pickerView.selectDateBlock = ^(NSString *selectedDate) {
        NSDate *date1 = [self.dateFormatter dateFromString:selectedDate];
        NSDate *date2 = [self.dateFormatter dateFromString:weakSelf.endDateLab.text];
        if ([date1 compare:date2] == NSOrderedDescending) {
            [Prompt popPromptViewWithMsg:@"开始日期不能大于截止日期" duration:2];
        }else {
            weakSelf.startDateLab.text = selectedDate;
            weakSelf.currentPage = 1;
            weakSelf.productSalesVolumeAPI.startTime = selectedDate;
            [weakSelf loadData];
        }
    };
}

- (void)clickEndDateBtn:(UIButton *)btn {
    WeakSelf
    HDDatePickerView *pickerView = [[HDDatePickerView alloc] initWithSlectedDate:self.endDateLab.text];
    [pickerView show];
    pickerView.selectDateBlock = ^(NSString *selectedDate) {
        NSDate *date1 = [self.dateFormatter dateFromString:selectedDate];
        NSDate *date2 = [self.dateFormatter dateFromString:weakSelf.startDateLab.text];
        if ([date1 compare:date2] == NSOrderedAscending) {
            [Prompt popPromptViewWithMsg:@"截止日期不能小于开始日期" duration:2];
        }else {
            weakSelf.endDateLab.text = selectedDate;
            weakSelf.currentPage = 1;
            weakSelf.productSalesVolumeAPI.endTime = selectedDate;
            [weakSelf loadData];
        }
    };
}

-(void)clickedScreeningBtn {
    self.moreView.hidden = YES;
    __weak typeof(self) weakSelf = self;
    SASupplierScreeningView3 *screenView = [SASupplierScreeningView3 shareCornView];
    screenView.delegate = self;
    [screenView loadData:self.screenArray SupplierName:self.providerName SupplierId:self.providerId];
    screenView.screeningBlock = ^(NSMutableArray *selectedArray, NSString *supplierName, NSString *supplierId) {
        [weakSelf.screenArray removeAllObjects];
        [weakSelf.screenArray addObjectsFromArray:selectedArray];
        if (supplierName.length > 0) {
            weakSelf.providerId = supplierId;
            weakSelf.providerName = supplierName;
        }else {
            weakSelf.providerId = @"";
            weakSelf.providerName = @"";
        }
        weakSelf.currentPage = 1;
        [weakSelf loadData];
    };
    [screenView showView:self];
}

-(void)clickedSortingBtn {
    self.moreView.hidden = YES;
    SAInventorySortView *sortView = [SAInventorySortView shareCornView];
    [sortView loadData:kTabBarTitle AndSelectNum:-1];
    UIViewController *vc = nil;
    [sortView showView:vc];
    WeakSelf
    sortView.selectBlock = ^(NSInteger selectNum) {
        [weakSelf selectedItem:selectNum + 100];
    };
}

-(void)showImage:(NSArray *)imageList {
    self.imageList = imageList;
    if (self.imageList.count > 0) {
        NSMutableArray *imageMarr = [NSMutableArray array];
        for (NSString *url in self.imageList) {
            NSString *newUrl = [url stringByReplacingOccurrencesOfString:@"?x-oss-process=style/thumbnail" withString:@""];
            NSString *imageUrl = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,l_720",newUrl];
            [imageMarr addObject:imageUrl];
        }
        self.imageList = imageMarr;
        YBImageBrowser *browser = [YBImageBrowser new];
        browser.dataSourceArray = self.imageList;
        browser.dataSource = self;
        browser.currentPage = 0;
        [browser show];
    }else {
        [Prompt popPromptViewWithMsg:@"该商品没有图片" duration:2];
    }
}

-(void)removeView {
    self.moreView.hidden = YES;
}

-(void)clickedMoreBtn {
    if (self.moreView.isHidden == YES) {
        self.moreView.hidden = NO;
    }else {
        self.moreView.hidden = YES;
    }
}

-(void)clickedScanBtn {
    [self.view endEditing:YES];
    [self.navigationController pushViewController:[[ScanHelper shareInstance] ScanVCWithStyle:notSquare qrResultCallBack:^(id result) {
        //扫码结果
        self.searchTf.text = result;
        self.currentPage = 1;
        [self loadData];
        [self.navigationController popViewControllerAnimated:NO];
    }] animated:YES];
}

//YBImageBrowserDataSource 代理实现赋值数据
- (NSInteger)yb_numberOfCellsInImageBrowser:(YBImageBrowser *)imageBrowser {
    return self.imageList.count;
}

- (id<YBIBDataProtocol>)yb_imageBrowser:(YBImageBrowser *)imageBrowser dataForCellAtIndex:(NSInteger)index {
    NSString *urlStr = [self.imageList objectAtIndex:index];
    YBIBImageData *data = [YBIBImageData new];
    data.imageURL = [NSURL URLWithString:urlStr];
    return data;
}

- (void)selectedItem:(NSInteger)tag {
    NSMutableArray *orderArray = [[NSMutableArray alloc] init];
    NSMutableDictionary *orderDict = [[NSMutableDictionary alloc] init];
    switch (tag - 100) {
        case 1:
            orderDict[@"key"] = @"salesCount";
            orderDict[@"value"] = @"desc";
            [orderArray addObject:orderDict];
            break;
        case 2:
            orderDict[@"key"] = @"salesCount";
            orderDict[@"value"] = @"asc";
            [orderArray addObject:orderDict];
            break;
        case 3:
            orderDict[@"key"] = @"salesAmount";
            orderDict[@"value"] = @"desc";
            [orderArray addObject:orderDict];
            break;
        case 4:
            orderDict[@"key"] = @"salesAmount";
            orderDict[@"value"] = @"asc";
            [orderArray addObject:orderDict];
            break;
        case 5:
            orderDict[@"key"] = @"profit";
            orderDict[@"value"] = @"desc";
            [orderArray addObject:orderDict];
            break;
        case 6:
            orderDict[@"key"] = @"profit";
            orderDict[@"value"] = @"asc";
            [orderArray addObject:orderDict];
            break;
        default:
            break;
    }
    self.productSalesVolumeAPI.order = @"";
    if (orderArray.count != 0) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:orderDict options:0 error:nil];
        NSString *strJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        self.productSalesVolumeAPI.order = strJson;
    }
    [self loadData];
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@""];
    [self setBgImage:nil];
    self.view.backgroundColor = kLineColor;
    [self addRightViews:@[self.topView]];
    [self.topView addSubview:self.searchTf];
    [self.topView addSubview:self.moreBtn];
    [self.topView addSubview:self.scanBtn];
    
    [self.view addSubview:self.topContentView];
    [self.topContentView addSubview:self.startLab];
    [self.topContentView addSubview:self.startDateLab];
    [self.topContentView addSubview:self.endLab];
    [self.topContentView addSubview:self.endDateLab];
    [self.topContentView addSubview:self.iconImageV];
    [self.topContentView addSubview:self.startDateBtn];
    [self.topContentView addSubview:self.endDateBtn];
    
    [self.topContentView addSubview:self.endDateBtn];
    
    [self.topContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kScaleNum(StatusHeight));
        make.height.mas_equalTo(kScaleNum(44));
        make.right.mas_equalTo(0);
        make.left.mas_equalTo(kScaleNum(0));
    }];
    
    [self.iconImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(29, 29));
    }];
    
    [self.startLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.startDateLab);
        make.top.mas_equalTo(kScaleNum(2));
        make.height.mas_equalTo(kScaleNum(20));
    }];
    
    [self.startDateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.iconImageV.mas_left).offset(kScaleNum(-68));
        make.top.mas_equalTo(kScaleNum(22));
        make.height.mas_equalTo(kScaleNum(20));
    }];
    
    [self.endLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.endDateLab);
        make.top.mas_equalTo(kScaleNum(2));
        make.height.mas_equalTo(kScaleNum(20));
    }];
    
    [self.endDateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageV.mas_right).offset(kScaleNum(68));
        make.top.mas_equalTo(kScaleNum(22));
        make.height.mas_equalTo(kScaleNum(20));
    }];
    
    [self.startDateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.startDateLab);
        make.top.bottom.mas_equalTo(0);
    }];
    
    [self.endDateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.endDateLab);
        make.top.bottom.mas_equalTo(0);
    }];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *timeFormatStr = [dateFormatter stringFromDate:[NSDate date]];
    _startDateLab.text = timeFormatStr;
    _endDateLab.text = timeFormatStr;
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.tipView];
    [self.view addSubview:self.moreView];
    
    self.currentPage = 1;
    self.productSalesVolumeAPI.pageSize = 20;
    self.productSalesVolumeAPI.startTime = timeFormatStr;
    self.productSalesVolumeAPI.endTime = timeFormatStr;
    self.productSalesVolumeAPI.merchantId = @([SAValueManger shareInStance].userInfo.merchantId).stringValue;
    self.productSalesVolumeAPI.urlType = self.type;
    [self loadData];
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage = 1;
        [weakSelf loadData];
    }];
    MJRefreshAutoStateFooter *mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage ++;
        [weakSelf loadData];
    }];
    [mj_footer setTitle:@"已经到底啦~" forState:MJRefreshStateIdle];//设置闲置状态下不显示“点击或上拉加载更多”
    self.tableView.mj_footer = mj_footer;
    
    [self.productGetConditionAPI sendRequestToServerWithActivityViewAndMask:GET];
}

#pragma mark - lazy
-(UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:SARectMake(0, 0, kBaseScreenWidth - 50, 44)];
    }
    return _topView;
}

-(UIButton *)scanBtn {
    if (!_scanBtn) {
        _scanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _scanBtn.frame = SARectMake(0, 0, 30, 44);
        [_scanBtn setImage:[UIImage imageNamed:@"home_scan"] forState:UIControlStateNormal];
        [_scanBtn addTarget:self action:@selector(clickedScanBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _scanBtn;
}

-(UITextField *)searchTf {
    if (!_searchTf) {
        _searchTf = [[UITextField alloc] initWithFrame:SARectMake(45, 7, 220, 30)];
        _searchTf.placeholder = @"输入商品款号/名称/条码";
        _searchTf.backgroundColor = [UIColor whiteColor];
        _searchTf.layer.cornerRadius = kScaleNum(5);
        _searchTf.layer.masksToBounds = YES;
        _searchTf.delegate = self;
        _searchTf.font = kFont(14);
        _searchTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        UIView *leftView = [[UIView alloc] initWithFrame:SARectMake(0, 0, 26, 30)];
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:SARectMake(6, 7, 14, 16)];
        imageV.image = [UIImage imageNamed:@"shop_search"];
        [leftView addSubview:imageV];
        _searchTf.leftView = leftView;
        _searchTf.leftViewMode = UITextFieldViewModeAlways;
        [_searchTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchTf;
}

-(UIButton *)moreBtn {
    if (!_moreBtn) {
        _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreBtn setImage:[UIImage imageNamed:@"shop_more"] forState:UIControlStateNormal];
        _moreBtn.frame = SARectMake(270, 0, 55, 44);
        [_moreBtn addTarget:self action:@selector(clickedMoreBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreBtn;
}

-(UIView *)moreView {
    if (!_moreView) {
        _moreView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _moreView.backgroundColor = kColor(0, 0, 0, 0.3);
        _moreView.hidden = YES;
        _moreView.userInteractionEnabled = YES;
        
        UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Inventory_moreView"]];
        imageV.frame = SARectMake(280, StatusHeight - 6, 90, 93);
        [_moreView addSubview:imageV];
        imageV.userInteractionEnabled = YES;
        
        [imageV addSubview:self.sortingBtn];
        [imageV addSubview:self.sortingLine];
        [imageV addSubview:self.screeningBtn];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeView)];
        [_moreView addGestureRecognizer:tap];
    }
    return _moreView;
}

-(UIButton *)sortingBtn {
    if (!_sortingBtn) {
        _sortingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sortingBtn.frame = SARectMake(0, 52, 90, 40);
        [_sortingBtn setTitle:@"销量排序" forState:UIControlStateNormal];
        _sortingBtn.titleLabel.font = kFont(15);
        [_sortingBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [_sortingBtn addTarget:self action:@selector(clickedSortingBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sortingBtn;
}

-(UIView *)sortingLine {
    if (!_sortingLine) {
        _sortingLine = [[UIView alloc] initWithFrame:SARectMake(0, 51, 90, 1)];
        _sortingLine.backgroundColor = kLineColor;
    }
    return _sortingLine;
}

-(UIButton *)screeningBtn {
    if (!_screeningBtn) {
        _screeningBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _screeningBtn.frame = SARectMake(0, 10, 90, 40);
        [_screeningBtn setTitle:@"销量筛选" forState:UIControlStateNormal];
        _screeningBtn.titleLabel.font = kFont(15);
        [_screeningBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [_screeningBtn addTarget:self action:@selector(clickedScreeningBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _screeningBtn;
}

- (UIView *)topContentView {
    if (!_topContentView) {
        _topContentView = [[UIView alloc] init];
        _topContentView.backgroundColor = [UIColor whiteColor];
    }
    return _topContentView;
}

- (UILabel *)startLab {
    if (!_startLab) {
        _startLab = [[UILabel alloc] init];
        _startLab.textColor = kBlackColor;
        _startLab.font = kFont(15);
        _startLab.text = @"开始";
    }
    return _startLab;
}

- (UILabel *)startDateLab {
    if (!_startDateLab) {
        _startDateLab = [[UILabel alloc] init];
        _startDateLab.textColor = kBlackColor;
        _startDateLab.font = kFont(15);
    }
    return _startDateLab;
}

- (UILabel *)endLab {
    if (!_endLab) {
        _endLab = [[UILabel alloc] init];
        _endLab.textColor = kBlackColor;
        _endLab.font = kFont(15);
        _endLab.text = @"截止";
    }
    return _endLab;
}

- (UILabel *)endDateLab {
    if (!_endDateLab) {
        _endDateLab = [[UILabel alloc] init];
        _endDateLab.textColor = kBlackColor;
        _endDateLab.font = kFont(15);
    }
    return _endDateLab;
}

- (UIImageView *)iconImageV {
    if (!_iconImageV) {
        _iconImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SA_to"]];
    }
    return _iconImageV;
}

- (UIButton *)startDateBtn {
    if (!_startDateBtn) {
        _startDateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_startDateBtn addTarget:self action:@selector(clickStartDateBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _startDateBtn;
}

- (UIButton *)endDateBtn {
    if (!_endDateBtn) {
        _endDateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_endDateBtn addTarget:self action:@selector(clickEndDateBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _endDateBtn;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 45), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight + 45)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SAStatisticsSaleCell class] forCellReuseIdentifier:@"SAStatisticsSaleCell"];
    }
    return _tableView;
}

-(UIView *)tipView {
    if (!_tipView) {
        _tipView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 44), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight + 44))];
        _tipView.backgroundColor = [UIColor whiteColor];
        UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Shop_search2"]];
        icon.frame = SARectMake(124, 106, 138, 83);
        [_tipView addSubview:icon];
        UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, icon.maxY + kScaleNum(20), _tipView.width, kScaleNum(20))];
        titleLab.text = @"暂无相关信息";
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.textColor = kGrayColor;
        [_tipView addSubview:titleLab];
    }
    return _tipView;
}

-(NSDateFormatter *)dateFormatter {
    if (!_dateFormatter) {
        _dateFormatter = [NSDateFormatter new];
        [_dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    return _dateFormatter;
}

-(NSMutableArray<SAProductSalesVolumeModel *> *)dataMarr {
    if (!_dataMarr) {
        _dataMarr = [NSMutableArray array];
    }
    return _dataMarr;
}

-(SAProductSalesVolumeAPI *)productSalesVolumeAPI {
    if (!_productSalesVolumeAPI) {
        _productSalesVolumeAPI = [SAProductSalesVolumeAPI apiWithDelegate:self];
    }
    return _productSalesVolumeAPI;
}

-(SAProductGetConditionAPI *)productGetConditionAPI {
    if (!_productGetConditionAPI) {
        _productGetConditionAPI = [SAProductGetConditionAPI apiWithDelegate:self];
    }
    return _productGetConditionAPI;
}

-(NSMutableArray *)screenArray {
    if (!_screenArray) {
        _screenArray = [NSMutableArray array];
    }
    return _screenArray;
}

@end
